package com.pc.ipushpin.business.service.impl;

import com.pc.ipushpin.business.dao.AppUserMapper;
import com.pc.ipushpin.business.dao.UserMapper;
import com.pc.ipushpin.business.model.AppUser;
import com.pc.ipushpin.business.model.User;
import com.pc.ipushpin.business.service.IAppUserService;
import com.pc.ipushpin.business.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service("appUserService")
public class AppUserService implements IAppUserService {

    @Resource
    private AppUserMapper mapper;

    @Override
    public int addUser(AppUser user) {
        return mapper.insert(user);
    }

    @Override
    public AppUser findUniqueBy(String propertyName, Object value) {
        return mapper.findUniqueBy(propertyName,value);
    }

    @Override
    public int save(AppUser user) {
        return mapper.save(user);
    }

    @Override
    public AppUser saveUser(String openId, String telephone) {
        AppUser appUser = mapper.findUniqueBy("wechatSpOpenId", openId);
        if(appUser != null) {
            appUser.setLastLoginTime(new Date());
            appUser.setWechatSpOpenid(openId);
            appUser.setUsername(telephone);
            appUser.setStatus(1);
        } else {
            appUser = new AppUser();
            appUser.setCreateTime(new Date());
            appUser.setLastLoginTime(new Date());
            appUser.setUsername(telephone);
            appUser.setWechatSpOpenid(openId);
            appUser.setStatus(1);
        }
        mapper.save(appUser);
        return appUser;
    }

    @Override
    public String getUserToken(String userId) {
        return mapper.getUserToken(userId);
    }


}
